package com.squid.cloud.common.util;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.taobao.api.DefaultTaobaoClient;
import com.taobao.api.TaobaoClient;
import com.taobao.api.request.AlibabaAliqinFcSmsNumSendRequest;
import com.taobao.api.response.AlibabaAliqinFcSmsNumSendResponse;

public class AliSmsUtil {
	
	protected Log log=LogFactory.getLog(this.getClass());
	
	private final static String url = "http://gw.api.taobao.com/router/rest";
	private String APP_KEY ;//= "23467716";
	private String APP_SECRET ;//= "1197cfe7eeca2fabc3e00903057493d0";
	
	public static void main(String[] args) throws Exception {
//		AliSmsUtil sms = new AliSmsUtil("23467716", "1197cfe7eeca2fabc3e00903057493d0");
//		Map<String,String> param = new HashMap<String, String>();
		//注册验证码：
        //模板ID：SMS_4480443
        //模板：验证码${code}，您正在注册成为${product}用户，感谢您的支持！
		/**
		param.put("sign", "延长壳牌四川");
		param.put("number", "13594190830");
		param.put("template", "SMS_4480443");
		LinkedHashMap<String, String> temp = new LinkedHashMap<String, String>(); 
		temp.put("code", "336598");
		temp.put("product", "延长壳牌（四川）石油有限公司");
		param.put("param", mapToString(temp));
		System.out.println(JsonUtils.objectToJson(param));
		sms.sendSMS(param);
		**/
		
		//换手机号码验证码：
		//模板ID： SMS_4480440
        //模板：验证码${code}，您正在尝试变更${product}重要信息，请妥善保管账户信息。
		/**
		param.put("sign", "延长壳牌四川");
		param.put("number", "13594190830");
		param.put("template", "SMS_4480440");
		LinkedHashMap<String, String> temp = new LinkedHashMap<String, String>(); 
		temp.put("code", "336598");
		temp.put("product", "延长壳牌（四川）石油有限公司");
		param.put("param", mapToString(temp));
		System.out.println(JsonUtils.objectToJson(param));
		sms.sendSMS(param);
		**/
		//退款通知站长： 
        //模板ID：SMS_16585166
        //模板：${station}油站在${time}时间发起一笔退款，退款金额${money}元，请注意查看。
		/**
		param.put("sign", "延长壳牌四川");
		param.put("number", "13594190830");
		param.put("template", "SMS_16585166");
		LinkedHashMap<String, String> temp = new LinkedHashMap<String, String>(); 
		temp.put("station", "蜀西路");
		temp.put("time", "2016-09-28");
		temp.put("money", "300.00");
		param.put("param", mapToString(temp));
		System.out.println(JsonUtils.objectToJson(param));
		sms.sendSMS(param);
		**/
		
		//专车认证审核通知
		//模板ID：SMS_16530166
        //模板：您的专车申请认证${status} ，请到会员中心查看。
		/**
		param.put("sign", "延长壳牌四川");
		param.put("number", "13594190830");
		param.put("template", "SMS_16530166");
		LinkedHashMap<String, String> temp = new LinkedHashMap<String, String>(); 
		temp.put("status", "通过");
		param.put("param", mapToString(temp));
		System.out.println(JsonUtils.objectToJson(param));
		sms.sendSMS(param);
		**/
		
		
		//优惠短信通知：
        //模板ID：SMS_16535109
        //模板：尊敬的客户，延长壳牌油站优惠活动开始了，详情请到会员中心查看，谢谢。 -回复T退订
		/**
		param.put("sign", "延长壳牌四川");
		param.put("number", "13594190830");
		param.put("template", "SMS_16535109");
		System.out.println(JsonUtils.objectToJson(param));
		sms.sendSMS(param);
		**/
	}
	
	
	public AliSmsUtil(String appKey, String secret){
		APP_KEY = appKey;
		APP_SECRET = secret;
	}
	
	public void sendSMS(Map<String,String> param) throws Exception{
		TaobaoClient client = new DefaultTaobaoClient(url, APP_KEY, APP_SECRET);
		AlibabaAliqinFcSmsNumSendRequest req = new AlibabaAliqinFcSmsNumSendRequest();
		req.setExtend("");
		req.setSmsType("normal");
		req.setSmsFreeSignName(param.get("sign"));
		req.setSmsParamString(param.get("param"));
		req.setRecNum(param.get("number"));
		req.setSmsTemplateCode(param.get("template"));
		AlibabaAliqinFcSmsNumSendResponse rsp = client.execute(req);
		log.info("======>阿里大于短信发送结果："+rsp.getBody());
		if(!rsp.isSuccess()){
			throw new Exception("短信发送失败");
		}
	}
	
	/**
	 * 将Map对象转换成阿里大鱼通用的字符串类型
	 * @return
	 */
	public static String mapToString(LinkedHashMap<String,String> param){
		StringBuffer str = new StringBuffer("{");
		if(null!= param){
			for(Map.Entry<String, String> entry : param.entrySet()) {
				str.append("\""+entry.getKey()+"\":\""+entry.getValue()+"\""+",");
			}
		}
		str.delete(str.length()-1,str.length() );
		str.append("}");
		return str.toString();
	}
}